package com.powerManager.sdk.handlers;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.jdbc.core.JdbcTemplate;

import com.powerManager.sdk.common.JdbcSingleton;
import com.powerManager.sdk.common.PagingResult;
import com.powerManager.sdk.utils.JsonUtil;

public class UserHandler {

	/** -- 用户模块,用户列表
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String loadListJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select * from sys_user";
		String page = request.getParameter("page");
		String pageSize = request.getParameter("pagesize");
		PagingResult pr = new PagingResult(sql);
		String json = null;
		try {
			json = pr.pagingToJson(Integer.parseInt(pageSize), Integer.parseInt(page));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(json);
		return "text:"+json;
	}
	
	
	
	
	/** --用户模块,用户详细,用户部门
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String userDeptJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select department_id,department_id id,department_name text,parent_id from sys_department";
		PagingResult pr = new PagingResult(sql);
		List<Map<String,Object>> jsonList = null;
		String json = null;
		try {
			jsonList = pr.noPagingTree(sql, null, "department_id", "parent_id", "0");
			json = JsonUtil.toJson(jsonList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "text:"+json;
	}
	
	
	public String getUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select * from sys_user where user_id=?";
		String id = request.getParameter("ID");
		JdbcTemplate jdbc =JdbcSingleton.getJdbcTemplate();
		Map<String, Object > jsonUser = new HashMap<String, Object>();
		try {
			Map<String, Object > user = jdbc.queryForMap(sql, id);
			jsonUser.put("Data", user);
			jsonUser.put("IsError", false);
			jsonUser.put("Message", "加载成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			jsonUser.put("IsError", true);
			jsonUser.put("Message", "加载失败!");
			e.printStackTrace();
		}
		return "text:"+JsonUtil.toJson(jsonUser);
	}
	
	public String getRole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select role_id as id,role_id as value,role_name as text from sys_role";
		PagingResult pr = new PagingResult(sql);
		List<Map<String,Object>> json = null;
		try {
			json = pr.noPaging(sql, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(json);
		return "text:"+JsonUtil.toJson(json);
	}
	
	public String addUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("addUser");
		String i_user_sql = "insert into sys_user(user_id,login_name,password,department_id," +
				"nickname,status,password_status,remark) values(GET_SEQ('SEQ_USER_ID','0'),?,?,?,?,?,?,?)";
		String i_role_sql = "insert into sys_role_user(id,user_id,role_id) values(GET_SEQ('SEQ_UROLE_ID','0'),GET_SEQ('SEQ_USER_ID','1'),?)";
		Object[] args = new Object[7];
		args[0] = request.getParameter("login_name");
		args[1] = request.getParameter("password");
		args[2] = request.getParameter("department_id");
		args[3] = request.getParameter("nickname");
		args[4] = request.getParameter("status");
		args[5] = request.getParameter("password_status");
		args[6] = request.getParameter("remark");
		JdbcTemplate jdbc =JdbcSingleton.getJdbcTemplate();
		//jdbc.
		int count = jdbc.update(i_user_sql, args);
		if(count>0){
			String role_id = request.getParameter("role_id");
			Object[] roles = role_id.split(",");
			if(roles.length>0){
				List<Object[]> myList = new ArrayList<Object[]>();
				for (Object r : roles) {
					myList.add(new Object[]{r});
				}
				jdbc.batchUpdate(i_role_sql, myList);
			}
				//jdbc.update(i_role_sql, roles);
		}
		return "text:true";
	}
	
	
	public String updateUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String u_user_sql="update sys_user set password=?,department_id=?," +
				"nickname=?,status=?,password_status=?,remark=? where user_id=?";
		String d_user_role="delete from sys_role_user where user_id=?";
		String i_role_sql = "insert into sys_role_user(id,user_id,role_id) values(GET_SEQ('SEQ_UROLE_ID','0'),?,?)";
		Object[] args = new Object[7];
		args[0] = request.getParameter("password");
		args[1] = request.getParameter("department_id");
		args[2] = request.getParameter("nickname");
		args[3] = request.getParameter("status");
		args[4] = request.getParameter("password_status");
		args[5] = request.getParameter("remark");
		args[6] = request.getParameter("user_id");
		JdbcTemplate jdbc =JdbcSingleton.getJdbcTemplate();
		jdbc.update(u_user_sql, args);
		jdbc.update(d_user_role, args[6]);
		String role_id = request.getParameter("role_id");
		Object[] roles = role_id.split(",");
		if(roles.length>0){
			List<Object[]> myList = new ArrayList<Object[]>();
			for (Object r : roles) {
				myList.add(new Object[]{args[6],r});
			}
			jdbc.batchUpdate(i_role_sql, myList);
		}
		return "text:true";
	}
	
	public String removeUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String u_user_sql="delete from sys_user where user_id=?";
		String d_user_role="delete from sys_role_user where user_id=?";
		Object[] args = new Object[1];
		args[0] = request.getParameter("ID");
		JdbcTemplate jdbc =JdbcSingleton.getJdbcTemplate();
		jdbc.update(u_user_sql, args);
		jdbc.update(d_user_role, args);
		return "text:true";
	}
	
	public String isExistUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sql="select count(1) from sys_user where login_name=?";
		String login_name = request.getParameter("login_name");
		JdbcTemplate jdbc =JdbcSingleton.getJdbcTemplate();
		int count = jdbc.queryForInt(sql, login_name);
		String json = "true";
		if(count>0){
			json="false";
		}
		System.out.println(json);
		return "text:"+json;
	}
}
